package core

import (
	"fmt"
	"global/constants"
	"github.com/garyburd/redigo/redis"
	"global/libs/structs"
	"encoding/json"
	"strings"
)

func GetHuobiApiUrl(uri string) string {
	return fmt.Sprintf("%s%s", constants.HuobiHttpApiBaseUrl, uri)
}

func GetHuobiMarketUrl(uri string) string {
	return fmt.Sprintf("%s%s", constants.HuobiMarketUrl, uri)
}

func GetBitZUrl(uri string) string {
	return fmt.Sprintf("%s/Market/%s", constants.BitzHttpBaseUrl, uri)
}

func GetBinanceApiV1Url(uri string) string {
	return fmt.Sprintf("%s/api/v1/%s", constants.BinanceHttpApiBaseUrl, uri)
}

func GetBitfinexV1Url(uri string) string {
	return fmt.Sprintf("%s/%s", constants.BitfinexV1Url, uri)
}

// 保存交易对到交易所 spider 下
func SaveSymbolsToExchange(conn redis.Conn, slug string, symbols map[string]*structs.Symbol) error {
	spiderKey := fmt.Sprintf(constants.SpiderSymbolPairKey, slug)
	conn.Send("MULTI")
	for _, v := range symbols {
		symbolStr, err := json.Marshal(v)
		if err != nil {
			conn.Do("DISCARD")
			return err
		}
		conn.Send("HSET", spiderKey, fmt.Sprintf("%s_%s",
			strings.ToLower(v.From), strings.ToLower(v.To)), string(symbolStr))
	}
	_, err := conn.Do("EXEC")
	return err
}

// 获取交易所获取到的交易对
func GetSymbolsFromExchange(conn redis.Conn, exchangeSlug string) (map[string]*structs.Symbol, error) {
	spiderKey := fmt.Sprintf(constants.SpiderSymbolPairKey, exchangeSlug)
	symbolStrs, err := redis.Strings(conn.Do("HVALS", spiderKey))
	if err != nil {
		return nil, err
	}

	if len(symbolStrs) == 0 {
		return nil, nil
	}

	symbols := make(map[string]*structs.Symbol)
	for _, v := range symbolStrs {
		var s *structs.Symbol
		if err := json.Unmarshal([]byte(v), &s); err != nil {
			continue
		}
		// symbols = append(symbols, s)
		from := strings.ToLower(s.From)
		to := strings.ToLower(s.To)
		key := fmt.Sprintf("%s_%s", from, to)
		symbols[key] = &structs.Symbol{
			From: 		from,
			To: 		to,
		}
	}

	return symbols, nil
}

// 从Cmc获取交易所交易对
func GetSymbolsFromCmc(conn redis.Conn, exchangeSlug string) (map[string]*structs.Symbol, error) {
	cmcKey := fmt.Sprintf(constants.CmcExchangePairKey, exchangeSlug)
	symbolStrs, err := redis.Strings(conn.Do("HVALS", cmcKey))
	if err != nil || len(symbolStrs) == 0 {
		return nil, err
	}

	symbols := make(map[string]*structs.Symbol, 0)
	for _, v := range symbolStrs {
		var s *structs.Symbol
		if err := json.Unmarshal([]byte(v), &s); err != nil {
			continue
		}
		from := strings.ToLower(s.From)
		to := strings.ToLower(s.To)
		key := fmt.Sprintf("%s_%s", from, to)
		symbols[key] = &structs.Symbol{
			From: 		from,
			To: 		to,
		}
	}
	return symbols, nil
}

// 通过交易所 slug 获取交易所所有交易对
// 如果交易所身有，从交易所获取，如果没有，从cmc获取
func GetSymbolsByExchange(conn redis.Conn, exchangeSlug string) ([]structs.Symbol, error) {
	var key string
	spiderKey := fmt.Sprintf(constants.SpiderSymbolPairKey, exchangeSlug)
	ok, err := redis.Bool(conn.Do("EXISTS", spiderKey))
	if err != nil {
		return nil, err
	}

	if !ok {
		cmcKey := fmt.Sprintf(constants.CmcExchangePairKey, exchangeSlug)
		ok, err := redis.Bool(conn.Do("EXISTS", cmcKey))
		if err != nil || !ok {
			return nil, err
		}
		key = cmcKey
	} else {
		key = spiderKey
	}

	symbolStrs, err := redis.Strings(conn.Do("HVALS", key))
	if err != nil {
		return nil, err
	}

	symbols := make([]structs.Symbol, 0)
	for _, v := range symbolStrs {
		var s structs.Symbol
		if err := json.Unmarshal([]byte(v), &s); err != nil {
			continue
		}
		symbols = append(symbols, s)
	}
	return symbols, nil
}